#if !defined(PointerHeader)
#define PointerHeader

#include "count.h"

template <typename T>
class ReferenceCountedPointer {
public:
  ReferenceCountedPointer () :
    node (0) {
  }

  ReferenceCountedPointer (T *e) :
    node (e) {
    if(e)
      countUp(e->referenceCount);
  }

  ReferenceCountedPointer (const ReferenceCountedPointer &p) :
    node (p.node) {
    if(p.node)
      countUp(p.node->referenceCount);
  }

  ReferenceCountedPointer (std::string&); // Parses from string
  ~ReferenceCountedPointer () {
    if(node && countDown(node->referenceCount) == 0)
      delete node;
  }

  operator T* () {
    return node;
  }

  ReferenceCountedPointer &operator = (const ReferenceCountedPointer &p) {
    if(p.node)
      countUp(p.node->referenceCount);
    if(node && countDown(node->referenceCount) == 0)
      delete node;
    node = p.node;
    return *this;
  }

  T *node;
};

#endif
